//
//  StatusCellTopView.swift
//  SinaWeibo
//
//  Created by Tony-ztb on 15/12/19.
//  Copyright © 2015年 Tony-ztb. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class StatusCellTopView: UIView {
    
    // 定义视图底部约束的属性
    var topViewBottomConstraints: Constraint?
    
    var status: Status? {
        didSet {
            iconView.sd_setImageWithURL(status?.user?.HeadImageURL)
            nameLabel.text = status?.user?.name
            mbrankImageView.image = status?.user?.mbrank_image
            verified_type_imageView.image = status?.user?.verified_type_image
            // TODO: 后续处理
//            timeLabel.text = status?.created_at
//            sourceLabel.text = status?.source
            contentLabel.text = status?.text
            
            pictureView.imageURLs = status?.imageURLs
            
            // 更新视图底部约束时, 先卸掉之前的底部约束
            self.topViewBottomConstraints?.uninstall()
            // 根据是否有配图  动态的调整约束关系
            if let urls = status?.imageURLs where urls.count > 0 {
                // 有配图
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.topViewBottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
            })
            } else {
                // 没有配图
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.topViewBottomConstraints = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置背景颜色 调试用
//        backgroundColor = UIColor(white: 0.9, alpha: 1)
        backgroundColor = UIColor.whiteColor()
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 自定义子视图
    private func setupUI() {
        addSubview(sepView)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(mbrankImageView)
        addSubview(verified_type_imageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        sepView.backgroundColor = UIColor(white: 0.8, alpha: 1)

        // 添加子视图约束
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self)
            make.height.equalTo(10)
        }
        
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: StatusCellHeadImageWidth, height: StatusCellHeadImageWidth))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        
        verified_type_imageView.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconView.snp_right)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
//            make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
        }
        
        
        // 配图视图的约束
        pictureView.snp_makeConstraints { (make) -> Void in
//            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
        }
        
        
        // 设置顶部视图的约束
        self.snp_makeConstraints { (make) -> Void in
            self.topViewBottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
        // 设置圆形头像
        iconView.layer.cornerRadius = StatusCellHeadImageWidth * 0.5
        iconView.layer.masksToBounds = true
    }
    
    
    // 懒加载所有子视图
    // 分割线视图
    private lazy var sepView: UIView = UIView()
    
    // 头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    // 名字
    private lazy var nameLabel: UILabel = UILabel(title: "哦希吧", color: themeColor, fontSize: 14)
    // 会员图标
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    // 认证图标
    private lazy var verified_type_imageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    // 微博创建时间
    private lazy var timeLabel: UILabel = UILabel(title: "12:12", color: themeColor, fontSize: 10)
    // 微博来源
    private lazy var sourceLabel: UILabel = UILabel(title: "风吹屁屁凉", color: themeColor, fontSize: 10)
    // 微博正文
    private lazy var contentLabel: UILabel = UILabel(title: "浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪,浪里个浪u`1", color: UIColor.darkGrayColor(), fontSize: 14, margin: StatusCellMargin)
    
    // 配图视图
    private lazy var pictureView: StatusCellPictureView = StatusCellPictureView()
}
